#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_task_wdt.h"

/*
看门狗
1. 中断看门狗，默认300ms
2. 任务看门狗，默认5s
*/

void myTask(void *pvParameters)
{
    while (1)
    {
        // 触发看门狗，idle watchdog
        // Task watchdog got triggered. The following tasks/users did not reset the watchdog in time
        // 因为idle watchdog 的优先级最低，是0,而在myTask中，没有调用任何阻塞函数譬如delay，导致idle watchdog没有被触发
        // vTaskDelay(pdMS_TO_TICKS(1000));
        // 或者将当前任务的优先级调至0,跟看门狗一样，就会轮流切换

        printf("myTask is running\n");
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

void myTask2(void *pvParameters)
{
    // 加入任务到watchdog监控列表，如果加入了监控列表，但是没有喂狗，也会触发
    esp_task_wdt_add(NULL);
    while (1)
    {
        printf("myTask2 is running\n");
        // 喂狗
        esp_task_wdt_reset();
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

void app_main(void)
{
    TaskHandle_t myTaskHandle;
    TaskHandle_t myTask2Handle;
    xTaskCreate(myTask, "myTask", 2048, NULL, 1, &myTaskHandle);
    xTaskCreate(myTask2, "myTask2", 2048, NULL, 1, &myTask2Handle);
}
